package com.zwt.workms.config;


import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

//国际化--视图解析器
public class MyLocaleResolver implements LocaleResolver {

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }

//    //解析请求
//    @Override
//    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
//
//        //获取请求中的语言参数
//        String language = httpServletRequest.getParameter("l");
//
//        Locale locale = Locale.getDefault();//如果没有使用默认
//
//        System.out.println("Debug======>" + language);
//
//        //如果不是空，就要使用
//        if (!StringUtils.isEmpty(language)) {
//            String[] split = language.split("_");
//            //分割出国家和地区
//            locale = new Locale(split[0], split[1]);
//        }
//
//        return locale;
//    }

    public Locale resolveLocale(HttpServletRequest request) {
        String l = request.getParameter("l");
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(l)) {
            String[] split = l.split("_");
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

}
